package com.colin.springbootsource.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Colin
 * 2019/12/22 13:58
 * @desc todo
 * @email zhaod@oceansoft.com.cn,colinzhaodong@gmail.com
 */
@Component
public abstract class AbstractEventMulticaster implements EventMulticaster{
    @Autowired
    private List<WeatherListener> listenerList;
    @Override
    public void multicastEvent(WeatherEvent event) {
        doStart();
        listenerList.forEach(weatherListener -> weatherListener.onWeatherEvent(event));
        doEnd();
    }

    @Override
    public void addListener(WeatherListener weatherListener) {
        listenerList.add(weatherListener);
    }

    @Override
    public void removeListener(WeatherListener weatherListener) {
        listenerList.remove(weatherListener);
    }
    abstract void doStart();
    abstract void doEnd();
}
